#include "StdAfx.h"
#include "MButton.h"
#include "../kernel.h"

extern Kernel* g_kernel;


namespace gui {

//MButton::MButton(void)
//{
//}
//
void MButton::OnClick()
{
    g_kernel->GetScriptEngine()->CallFunction( m_onClick );
}

/** Set background Image */
void MButton::SetBackgroundImage( Image image, int fps, int amt, int row, int col )
{
    m_normalAnim.Set( fps, row, col, amt );
    m_backgroundImage = image;
    m_bgImageInit = true;
}

/** Set background Image */
void MButton::SetHoverImage( Image image, int fps, int amt, int row, int col )
{
    m_hoverAnim.Set( fps, row, col, amt );
    m_hoverImage = image;
    m_hvImageInit = true;
}

/** Update this component */
void MButton::Update( long dt )
{
    m_normalAnim.Update( dt );
    m_hoverAnim.Update( dt );

    // now draw all sub components
    gui::Component::Update( dt );
}

/** Render this bad boy */
void MButton::Render( IRender* r )
{
    Vector3f bg = GetBackgroundColor();
    Vector3f fg = GetForegroundColor();

    // set background color
    r->SetColor( bg.GetX(), bg.GetY(), bg.GetZ(), GetAlpha() );

    Vector2f pos = GetLocation();
    r->FillRect( pos.x, pos.y, GetWidth(), GetHeight() );

    if ( m_bgImageInit )
    {
        r->SetColorEffect( 1, 1, 1, 1 );
        r->DrawSubImage( &m_backgroundImage, pos.x, pos.y, m_normalAnim.GetRow(), m_normalAnim.GetFrame() ); 
    }

    // draw border
    r->SetColor( 0, 0, 0 );
    r->DrawRect( pos.x, pos.y, GetWidth(), GetHeight() );
    if ( m_Hover )
    {
        r->SetColor( 1, 1, 1 );
        r->DrawRect( pos.x-1, pos.y+1, GetWidth(), GetHeight() );
        if ( m_hvImageInit )
        {
            r->SetColorEffect( 1, 1, 1, 1 );
            r->DrawSubImage( &m_hoverImage, pos.x-1, pos.y+1, m_hoverAnim.GetRow(), m_hoverAnim.GetFrame() ); 
        }
    }
    

    int charWidth = r->GetTextWidth( 'H' );
    int length = charWidth * m_text.length();

    std::string str;
    if ( length > GetWidth() )
    {
        int remaining = (GetWidth() - length) / charWidth;
        int npos = m_text.length() - remaining;
        str = m_text.substr( 0, npos );
    } 
    else
    {
        str = m_text;
    }
    r->SetColor( fg.GetX(), fg.GetY(), fg.GetZ() );
    r->DrawString( pos.x, pos.y, str.c_str() );

    // now draw all sub components
    gui::Component::Render( r );
}

/** Handle input */
bool MButton::HandleInput()
{
    if ( !HasFocus() )
        return false;

    Vector2f pos = GetLocation();
    Rect b( pos.x, pos.y, GetWidth(), GetHeight() );

    int x = g_kernel->GetInput()->GetMouseX();
    int y = g_kernel->GetInput()->GetMouseY();

    if ( b.Intersects( x, y ) )
    {
        m_Hover = true;
        if ( g_kernel->GetInput()->IsButtonDown( 1 ) ) {
            OnClick();
            return true;
        }
    }
    else
    {
        m_Hover = false;
    }

    // Check to see if we have a joystick
    if ( g_kernel->GetInput()->JoystickEnabled() )
    {
        int x = g_kernel->GetInput()->GetJoystickX();
        int y = g_kernel->GetInput()->GetJoystickY();
        if ( b.Intersects( x, y ) )
        {
            m_Hover = true;
            if ( g_kernel->GetInput()->IsJoystickButtonDown( 1 ) ) {
                OnClick();
                return true;
            }
        }
        else
        {
            m_Hover = false;
        }
    }
    // now handle input on all sub components
    return gui::Component::HandleInput();
}

};